What is string-trim-spaces-only?
The string-trim-spaces-only npm package is a utility for trimming spaces from strings. It focuses on removing only the leading and trailing spaces, leaving the inner spaces intact.
What are string-trim-spaces-only's main functionalities?
Trim leading and trailing spaces
This feature removes the leading and trailing spaces from a string, but keeps the spaces between words intact.
const trimSpaces = require('string-trim-spaces-only');
const result = trimSpaces(' Hello World ');
console.log(result); // 'Hello World'
Trim leading spaces only
This feature removes only the leading spaces from a string, leaving the trailing spaces and inner spaces intact.
const trimSpaces = require('string-trim-spaces-only');
const result = trimSpaces(' Hello World ', { leading: true, trailing: false });
console.log(result); // 'Hello World '
Trim trailing spaces only
This feature removes only the trailing spaces from a string, leaving the leading spaces and inner spaces intact.
const trimSpaces = require('string-trim-spaces-only');
const result = trimSpaces(' Hello World ', { leading: false, trailing: true });
console.log(result); // ' Hello World'
Other packages similar to string-trim-spaces-only
trim
The trim package is a simple utility to trim leading and trailing whitespace from a string. It is similar to string-trim-spaces-only but does not offer options to trim only leading or trailing spaces.
lodash.trim
The lodash.trim package is part of the Lodash library and provides a method to trim leading and trailing whitespace from a string. It is similar to string-trim-spaces-only but is part of a larger utility library with many other functions.
validator
The validator package includes a trim function that removes leading and trailing whitespace from a string. It is similar to string-trim-spaces-only but is part of a comprehensive library for string validation and sanitization.
string-trim-spaces-only
Like String.trim() but you can choose granularly what to trim
Install
This package is pure ESM. If you're not ready yet, install an older version of this program, 3.1.0 (npm i string-trim-spaces-only@3.1.0
).
npm i string-trim-spaces-only
Quick Take
import { strict as assert } from "assert";
import { trimSpaces } from "string-trim-spaces-only";
assert.deepEqual(trimSpaces(" aaa "), {
res: "aaa",
ranges: [
[0, 2],
[5, 8],
],
});
assert.deepEqual(trimSpaces(" \t zz \n "), {
res: "\t zz \n",
ranges: [
[0, 3],
[12, 16],
],
});
Documentation
Please visit codsen.com for a full description of the API.
Contributing
To report bugs or request features or assistance, raise an issue on GitHub.
Licence
MIT License.
Copyright © 2010-2022 Roy Revelt and other contributors.